<template>
  <div class="app-container">
    <el-form
      :model="oilQuery"
      ref="oilQuery"
      :inline="true"
      v-show="showSearch"
      class="globalSerchFormStyle"
    >
      <el-row>
        <el-col :span="6">
          <el-form-item label="品名" prop="oilName">
            <el-input
              v-model="oilQuery.oilName"
              placeholder="请输入油企名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
              style="width: 190px"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row :gutter="10" class="global_el_rowStyle">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
          type="primary"
          plain
          >重置</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button plain size="mini" @click="handlePayAdd">
          新增油品
        </el-button>
      </el-col>
    </el-row>
    <!-- <el-card class="card-table">
      <el-row :gutter="10" class="mb8">
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row> -->
    <el-table :data="oilList" style="width: 100%" v-loading="oilLoading" stripe>
      <el-table-column prop="oilName" label="品名" align="left">
      </el-table-column>
      <el-table-column prop="oilClass" label="类型" align="left">
      </el-table-column>
      <el-table-column prop="oilType" label="型号" align="left">
      </el-table-column>
      <el-table-column prop="unitCode" label="单位" align="left">
        <template slot-scope="scope">
          {{ scope.row.unitCode == "1" ? "L/kg" : "" }}
          {{ scope.row.unitCode == "2" ? "千克" : "" }}
          {{ scope.row.unitCode == "3" ? "公斤" : "" }}
        </template>
      </el-table-column>
      <el-table-column prop="cardType" label="单位" align="left">
        <template slot-scope="scope">
          {{ scope.row.cardType == "1" ? "汽油卡" : "" }}
          {{ scope.row.cardType == "2" ? "柴油卡" : "" }}
          {{ scope.row.cardType == "3" ? "LNG卡" : "" }}
          {{ scope.row.cardType == "4" ? "CNG卡" : "" }}
        </template>
      </el-table-column>

      <el-table-column label="操作" align="left">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handlePayEdit(scope.row)"
            size="mini"
            style="margin: 0px 5px"
            >编辑</el-button
          >
          <el-button
            type="text"
            @click="handlePayDel(scope.row)"
            size="mini"
            style="margin: 0px 5px"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- </el-card> -->
    <pagination
      :hidden="oilListTotal > 0 ? false : true"
      :total="oilListTotal"
      :page.sync="oilQuery.pageNum"
      :limit.sync="oilQuery.pageSize"
      @pagination="getList"
      :autoScroll="true"
    />

    <el-dialog
      :title="'添加油品'"
      :visible.sync="oilOpen"
      width="800px"
      append-to-body
    >
      <el-form
        ref="oilForm"
        :model="oilForm"
        :rules="rules"
        label-width="80px"
        label-position="top"
      >
        <el-row>
          <el-col :span="11">
            <el-form-item
              label="品名"
              prop="oilName"
              :rules="{
                required: true,
                message: '请填写油品名',
                trigger: 'change',
              }"
            >
              <el-input v-model="oilForm.oilName" placeholder="请输入品名" />
            </el-form-item>
          </el-col>

          <el-col :span="11" :offset="1">
            <el-form-item
              label="类型"
              prop="oilClass"
              :rules="{
                required: true,
                message: '请填写油品类型',
                trigger: 'change',
              }"
            >
              <!-- <el-input v-model="oilForm.oilClass" placeholder="请输入油品类型" /> -->
              <el-select
                v-model="oilForm.oilClass"
                placeholder="请选择油品类型"
                style="width: 100%"
              >
                <el-option
                  v-for="item in oilClassList"
                  :key="item"
                  :label="item"
                  :value="item"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="11">
            <el-form-item
              label="型号"
              prop="oilType"
              :rules="{
                required: true,
                message: '请填写油品型号',
                trigger: 'change',
              }"
            >
              <el-input v-model="oilForm.oilType" placeholder="请输入型号" />
            </el-form-item>
          </el-col>

          <el-col :span="11" :offset="1">
            <el-form-item
              label="单位"
              prop="unitCode"
              :rules="{
                required: true,
                message: '请填写计量单位',
                trigger: 'change',
              }"
            >
              <el-select
                v-model="oilForm.unitCode"
                placeholder="请选择单位"
                clearable
                :style="{ width: '100%' }"
              >
                <el-option label="(L/kg)" value="1"></el-option>
                <el-option label="千克" value="2"></el-option>
                <el-option label="公斤" value="3"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="23">
            <el-form-item
              label="可用的电子卡类别"
              prop="cardType"
              :rules="{
                required: true,
                message: '请填写计量单位',
                trigger: 'change',
              }"
            >
              <el-select
                v-model="oilForm.cardType"
                placeholder="请选择电子卡类型"
                clearable
                :style="{ width: '100%' }"
              >
                <el-option label="汽油卡" value="1"></el-option>
                <el-option label="柴油卡" value="2"></el-option>
                <el-option label="LNG卡" value="3"></el-option>
                <el-option label="CNG卡" value="4"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="submitForm"
          >确 定</el-button
        >
        <el-button size="mini" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { regionData } from "element-china-area-data";
import { getToken } from "@/utils/auth";

import { getOilList, addOil, upOil, delOil } from "@/api/dept";

export default {
  name: "Dept",
  components: { Treeselect },
  data() {
    return {
      regionDataOptions: regionData, // 地址组件
      selectedRegionData: [],
      // 显示搜索条件
      showSearch: true,
      // 弹出层标题
      title: "",
      splitShow: false,
      // 状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        deptName: undefined,
        status: undefined,
      },
      // 表单校验
      rules: {},

      oilList: [],
      oilLoading: false,
      imgUpConfig: {
        headers: {
          Authorization: "Bearer " + getToken(),
        },
        addressUrl: process.env.VUE_APP_BASE_API + "/common/upload",
        limit: 5,
      },
      oilOpen: false,
      oilForm: {
        oilClass: "", //类型
        oilName: "", // 品名
        oilType: "", //型号
        unitCode: "1", // 单位 1L 2kg 3公斤
        cardType: "1",
      },
      oilQuery: {
        pageSize: 10,
        pageNum: 1,
        oilName: "",
      },
      oilListTotal: 0,
      oilClassList: ["汽油", "柴油", "LNG", "CNG", "便利", "服务"],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handlePayAdd() {
      this.oilForm = {};
      this.oilOpen = true;
      this.$nextTick(() => {
        this.$refs.oilForm.clearValidate();
      });
    },
    handlePayDel(row) {
      this.$confirm("确认删除此油品？", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delOil(row);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除油品成功");
        });
    },
    handlePayEdit(row) {
      this.oilForm = JSON.parse(JSON.stringify(row));
      this.oilOpen = true;
    },
    getList() {
      this.oilLoading = true;
      getOilList(this.oilQuery)
        .then((res) => {
          this.oilListTotal = res.total;
          this.oilList = res.rows;
        })
        .finally(() => (this.oilLoading = false));
    },
    cancel() {
      this.oilOpen = false;
    },
    reset() {
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      console.log(11111111111);
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("oilQuery");
      this.handleQuery();
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["oilForm"].validate((valid) => {
        if (valid) {
          if (this.oilForm.oilId != undefined) {
            upOil(this.oilForm).then((res) => {
              this.msgSuccess("更新成功");
              this.getList();
              this.oilOpen = false;
            });
          } else {
            addOil(this.oilForm).then((res) => {
              this.msgSuccess("新增成功");
              this.getList();
              this.oilOpen = false;
            });
          }
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.card-table {
  margin-top: 40px;
  background-color: #f8f8f8;

  .el-button {
    margin-bottom: 20px;
    margin-right: 20px;
  }
}

::v-deep .el-form-item {
  .el-form-item__label {
    padding: 0;
    margin: 0;
  }

  .el-button--cyan {
    margin-left: 20px;
  }
}
</style>
